package org.spl.elevator.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import java.util.ArrayList;

import org.spl.elevator.model.PanelDAO;

public class ButtonsInternalPanel extends JPanel implements ActionListener{
	private ButtonEl buttons[];
	private int maxSelected;
	private boolean openDoorButton = false;
	private boolean closeDoorButton = false;
	private boolean alarmButton = false;
	private ArrayList<Integer> levelList = new ArrayList<Integer>(); 
	private int quantA = 4;
	private boolean blockedLevelPressed = false;
	
	public ButtonsInternalPanel(PanelDAO panel){
		quantA = quantA + panel.getNumButtons(); 
		maxSelected = panel.getMaxSelectedButton();
		
		this.buttons = new ButtonEl[quantA];		
		this.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		
		int linha = 1;
		int coluna = 0;
		
		for(int i = panel.getNumButtons(); i > 0; i--){
			buttons[i] = new ButtonEl(""+i+"");
			buttons[i].setBackground(java.awt.Color.LIGHT_GRAY);
			buttons[i].addActionListener(this);
			buttons[i].setColoringButton(panel.isColoringButton());			
			buttons[i].setPassword("1"+i);
			buttons[i].setBlockLevel(true);
			c.fill = GridBagConstraints.HORIZONTAL;
			c.insets = new Insets(2,2,2,2);
			c.ipadx = 50;
			c.weightx = 0.5;
			c.gridx = coluna;		
			c.gridy = linha;
			this.add(buttons[i], c);
			
			coluna++;
			
			if(coluna == 3){
				coluna = 0;
				linha++;
			}
		}
		
		buttons[0] = new ButtonEl("PG");
		buttons[0].setBackground(java.awt.Color.LIGHT_GRAY);
		buttons[0].addActionListener(this);
		buttons[0].setColoringButton(panel.isColoringButton());
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = coluna;		
		c.gridy = linha;
		this.add(buttons[0], c);
				
		GridBagConstraints c2 = new GridBagConstraints();
		c2.fill = GridBagConstraints.HORIZONTAL;
		
		buttons[panel.getNumButtons()+1] = new ButtonEl("<>");
		buttons[panel.getNumButtons()+1].setBackground(java.awt.Color.GRAY);
		buttons[panel.getNumButtons()+1].addActionListener(this);
		buttons[panel.getNumButtons()+1].setColoringButton(panel.isColoringButton());
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.insets = new Insets(2,2,2,2);
		c2.weightx = 0.5;
		c2.gridx = 0;		
		c2.gridy = linha + 1;
		this.add(buttons[panel.getNumButtons()+1], c2);
		
		buttons[panel.getNumButtons()+2] = new ButtonEl("Alarme");
		buttons[panel.getNumButtons()+2].setBackground(java.awt.Color.RED);
		buttons[panel.getNumButtons()+2].addActionListener(this);
		buttons[panel.getNumButtons()+2].setColoringButton(panel.isColoringButton());
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.insets = new Insets(2,2,2,2);
		c2.weightx = 0.5;
		c2.gridx = 1;		
		c2.gridy = linha + 1;
		this.add(buttons[panel.getNumButtons()+2], c2);
		
		buttons[panel.getNumButtons()+3] = new ButtonEl("><");
		buttons[panel.getNumButtons()+3].setBackground(java.awt.Color.GRAY);
		buttons[panel.getNumButtons()+3].addActionListener(this);
		buttons[panel.getNumButtons()+3].setColoringButton(panel.isColoringButton());
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.insets = new Insets(2,2,2,2);
		c2.weightx = 0.5;
		c2.gridx = 2;		
		c2.gridy = linha + 1;
		this.add(buttons[panel.getNumButtons()+3], c2);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().substring(0).equals("<>")){
			openDoorButton = true;
		} else if(e.getActionCommand().substring(0).equals("><")){
			closeDoorButton = true;
		} else if(e.getActionCommand().substring(0).equals("Alarme")){
			alarmButton = true;
		} else if(e.getActionCommand().substring(0).equals("PG")){
			if(levelList.size() < maxSelected){
				buttons[0].changeColor();
				levelList.add(0);
			}
		} else {
			int who = Integer.valueOf(e.getActionCommand().substring(0)).intValue();
			if(levelList.size() < maxSelected){
				if(buttons[who].isBlockLevel() == false){
					buttons[who].changeColor();
					levelList.add(who);
					blockedLevelPressed = false;
				} else {
					blockedLevelPressed = true;
				}
			}
		}
	}
	
	public void setOpenDoorButton(boolean openDoorButton) {
		this.openDoorButton = openDoorButton;
	}

	public void setCloseDoorButton(boolean closeDoorButton) {
		this.closeDoorButton = closeDoorButton;
	}

	public void setAlarmButton(boolean alarmButton) {
		this.alarmButton = alarmButton;
	}

	public ArrayList<Integer> getLevelList(){
		return levelList;
	}
	
	public void deselectButtono(int who){
		buttons[who].changeColor(java.awt.Color.LIGHT_GRAY);
	}	

	public boolean isOpenDoorButton() {
		return openDoorButton;
	}

	public boolean isCloseDoorButton() {
		return closeDoorButton;
	}

	public boolean isAlarmButton() {
		return alarmButton;
	}
	
	public ButtonEl getButton(int who){
		return buttons[who];
	}
	
	public ButtonEl[] getButtonsList(){
		return buttons;
	}
	
	public boolean isBlockedLevelPressed(){
		return blockedLevelPressed;
	}
	
	public void setBlockedLevelPressed(boolean pressed){
		blockedLevelPressed = pressed;
	}
}
